package Components.image
{
	import Components.config.ImageConfig;
	import Components.image.frame.BitmapMovieClip;
	import Components.progress.ProgressController;
	
	import core.RefreshManager;
	
	import events.GlobalEvent;
	import events.SDispatcher;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.filesystem.File;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	import flash.utils.setTimeout;
	
	import utils.FileUtils;

	public class ImageController
	{
		private const DEFAULT:String = "默认";
		
		private var _hasShow:Boolean = false;
		private var _container:Sprite;
		private var _parent:DisplayObjectContainer;

		private var _openDir:String = "";
		private var _fileName:String = "";

		private var _arrActions:Array;
		
		private var _currentBM:BitmapMovieClip;
		private var _dictBM:Dictionary;
		private var _vecBM:Vector.<BitmapMovieClip>;
		
		private var _opening:TextField;
		
		public function ImageController( parent:DisplayObjectContainer )
		{
			_parent = parent;
			init();
		}
		
		private function init():void
		{
			_container = new Sprite();
			_parent.addChild( _container );
			_vecBM = new Vector.<BitmapMovieClip>();
			
			_opening = new TextField();
			_opening.textColor = 0xffffff;
			_opening.text = "opening...";
			_opening.filters = [new GlowFilter(0,1,2,2,5)];
			_opening.width = _opening.textWidth + 4;
			_opening.height = _opening.textHeight + 4;
			_opening.visible = false;
			_container.addChild( _opening );
		}
		
		public function clear():void
		{
			ImageConfig.arrOutBMClass.length = 0;
			
			_arrActions = new Array();
			
			while( _vecBM.length > 0 )
			{
				_vecBM.pop().destroy();
			}
			_dictBM = null;
			_currentBM = null;
		}
		
		public function setLocation( x:int, y:int ):void
		{
			_container.x = x;
			_container.y = y;
		}
		
		public function open( dir:String ):void
		{
			clear();
			
			_dictBM = new Dictionary();
			ImageConfig.total_bitmap = 0;
			_opening.visible = true;
			
			_hasShow = false;
			_openDir = dir;
			var file:File = new File( dir );
			_fileName = file.name;
			ImageConfig.out_dir = File.applicationDirectory.nativePath + "/output/" + _fileName + "/";
			
			var arr:Array = file.getDirectoryListing();
			parseAllActions( arr );
			
			if( _arrActions.length > 0 )
			{
				_currentParseIndex = 0;
				parseNextAction();
			}
			else
			{
				parseSingleAction( _openDir, DEFAULT );
				SDispatcher.dispatchEvent( new GlobalEvent( GlobalEvent.ALL_IMAGE_LOADED ) );
			}
		}
		
		private function parseAllActions( arr:Array ):void
		{
			var f:File;
			var action:String = "";
			for( var i:int=arr.length; i>0; i-- )
			{
				f = arr.pop();
				if( f.isDirectory && ActionFileName.hasActionFile( f.name ) )
				{
					if( action=="" ) action = f.name;
					_arrActions.push( f.name );
				}
			}
			
			if( action=="" ) 
			{
				action = DEFAULT;
			}
			ImageConfig.currentAction = action;
			
			SDispatcher.dispatchEvent( new GlobalEvent( GlobalEvent.CURRENT_FILE_ACTIONS, _arrActions as Object ) );
		}
		
		private var _currentParseIndex:int = 0;
		private function parseNextAction():void
		{
			if( _currentParseIndex < _arrActions.length )
			{
				parseSingleAction( _openDir + "/" + _arrActions[_currentParseIndex], _arrActions[_currentParseIndex] );
				
				_currentParseIndex++;
				setTimeout( parseNextAction, 30 );
			}
			else
			{
				SDispatcher.dispatchEvent( new GlobalEvent( GlobalEvent.ALL_IMAGE_LOADED ) );
			}
		}
		
		private function parseSingleAction( dir:String, action:String ):void
		{
			var file:File = new File( dir );
			var arr:Array = file.getDirectoryListing();
			
			var f:File;
			for( var i:int=arr.length; i>0; i-- )
			{
				f = arr.pop();
				if( f.isDirectory && ActionFileName.hasDirectFile( f.name ) )
				{
					var bm:BitmapMovieClip = new BitmapMovieClip( _container );
					bm.load( f.nativePath, action, f.name );
					_vecBM.push( bm );
					_dictBM[action + "_" +f.name] = bm;
				}
			}
			if( !_hasShow )
			{
				_opening.visible = false;
				_hasShow = true;
				show( "上" );
			}
		}
		
		public function show( direction:String ):void
		{
			if( _dictBM==null ) return;
			
			if( _currentBM != null ) _currentBM.close();
			var str:String = ImageConfig.currentAction + "_" + direction;
			_currentBM = _dictBM[str];
			if( _currentBM )
			{
				_currentBM.show();
			}
		}
		
		public function play():void
		{
//			if( _currentBM ) _currentBM.play();
			RefreshManager.instance.start();
		}
		
		public function stop():void
		{
			RefreshManager.instance.stop();
		}
		
		public function nextFrame():void
		{
			if( _currentBM ) _currentBM.nextFrame();
		}
		
		private var _currentIndex:int = 0;
		private var _progress:ProgressController = new ProgressController();
		/**
		 * 输出资源 
		 */		
		public function output():void
		{
			ImageConfig.outAS = "";
			ImageConfig.outBMDirArray = "";
			
			_currentIndex = 0;
			ImageConfig.lastOutAction = "";
			if( _vecBM.length > 0 )
			{
				_progress.show();
				outBitmap();
			}
		}
		
		private function outBitmap():void
		{
			if( _currentIndex < _vecBM.length )
			{
				_vecBM[_currentIndex].output();
				_currentIndex++;
				setTimeout( outBitmap, 30 );
			}
			else
			{
				outSwf();
			}
		}
		
		private function outSwf():void
		{
			var jsfl:String = ImageConfig.JSFL;
			var str:String = ImageConfig.out_dir+"/image/";
			str = FileUtils.getURI( str );
			jsfl = jsfl.replace( "_DIR_", str );
			str = ImageConfig.out_dir + "/" + _fileName;
			str = FileUtils.getURI( str );
			jsfl = jsfl.replace( "_SAVE_PATH_", str );
			jsfl = jsfl.replace( "_CONFIG_", ImageConfig.outAS );
			
			FileUtils.saveAndOpen( File.applicationDirectory.nativePath + "/tmp/tmp.jsfl", jsfl );
			
			setTimeout( outComplete, 100 );
		}
		
		private function outComplete():void
		{
			SDispatcher.dispatchEvent( new GlobalEvent( GlobalEvent.OUT_COMPLETE ) );
		}
	}
}















